package com.bossly.contactsflag.activities;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class GroupedAdapter extends BaseAdapter {

	public ContactsAdapter contacts;
	public SimpleCursorAdapter agregations;
	public int contactID;

	int TYPE_HEADER = 0;
	int TYPE_ACCOUNT = 1;
	int TYPE_AGREGATION = 2;

	@Override
	public int getCount() {
		return contacts.getCount() + agregations.getCount() + 2; // 2 headers
	}

	Context _context;

	public GroupedAdapter(Context context) {
		// TODO Auto-generated constructor stub
		_context = context;
	}

	private long getPrimaryRawContact(long contactId) {
		long primary = -1;
		ContentResolver cr = _context.getContentResolver();

		String where = Data.CONTACT_ID + " = " + contactID + " AND "
				+ Data.IS_PRIMARY + " = 1";

		Cursor cursor = cr.query(Data.CONTENT_URI, null, where, null, null);
		int idIdx = cursor.getColumnIndexOrThrow(Data.RAW_CONTACT_ID);
		if (cursor.moveToFirst()) {
			primary = cursor.getInt(idIdx);
		}
		cursor.close();

		return primary;
	}

	@Override
	public Object getItem(int position) {

		int contacts_count = contacts.getCount();

		if (position == 0 || contacts_count == position + 1) {
			return "header";
		} else if (contacts_count > position) {
			return contacts.getItem(position - 1);
		} else {
			Cursor c = (Cursor) agregations.getItem(position - contacts_count
					- 2);
			String[] names = c.getColumnNames();
			AgregationTag ta = new AgregationTag();
			int pr = c.getInt(c.getColumnIndex(RawContacts._ID));
			ta.rawcontactId = (int) getPrimaryRawContact(pr);

			ta.gcontactId = (int) getPrimaryRawContact(contactID);

			return ta;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		TextView text = null;

		if (convertView == null) {
			int contacts_count = contacts.getCount();

			if (position == 0 || contacts_count == position - 1) {
				text = new TextView(parent.getContext());
				text.setText("header");
				convertView = text;
			} else if (contacts_count > position - 1) {
				return contacts.getView(position - 1, convertView, parent);
			} else {
				return agregations.getView(position - contacts_count - 2,
						convertView, parent);
			}
		}

		// text = (TextView) convertView;

		return convertView;
	}

	@Override
	public int getItemViewType(int position) {

		int contacts_count = contacts.getCount();

		if (position == 0 || contacts_count == position + 1) {
			return TYPE_HEADER;
		} else if (contacts_count > position) {
			return TYPE_ACCOUNT;
		} else {
			return TYPE_AGREGATION;
		}

		// return super.getItemViewType(position);
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;// super.getViewTypeCount();
	}

}
